﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Lswweb.Model;
using Lswweb.IDAL;


namespace Lswweb.SqlServerDAL
{
    public class LinkDAL : ILink
    {
        #region 构造方法、字段、映射方法

        private Builder<LinkInfo> linkDAL = new Builder<LinkInfo>("Link","Id");

        public LinkDAL()
        {
            linkDAL.CreateModel += new ReaderBind<LinkInfo>(linkDAL_CreateModel);
            linkDAL.GetParameters += new ParametersBuilder<LinkInfo>(linkDAL_GetParameters);
        }

        IDataParameter[] linkDAL_GetParameters(LinkInfo model)
        {
            return new IDataParameter[]{
                DataAccess.CreateParameter("Id",model.Id),
                DataAccess.CreateParameter("ItemId",model.ItemId),
                DataAccess.CreateParameter("Title",model.Title),
                DataAccess.CreateParameter("Url",model.Url),
                DataAccess.CreateParameter("ImageUrl",model.ImageUrl),
                DataAccess.CreateParameter("DisplayOrder",model.DisplayOrder),
                DataAccess.CreateParameter("Description",model.Description),
                DataAccess.CreateParameter("Status",model.Status),
                DataAccess.CreateParameter("Ver",model.Ver),
            };
        }

        LinkInfo linkDAL_CreateModel(Hashtable dataReader)
        {
            LinkInfo model = new LinkInfo();

            if (dataReader.ContainsKey("Id"))
                model.Id = Convert.ToInt32(dataReader["Id"]);

            if (dataReader.ContainsKey("ItemId"))
                model.ItemId = Convert.ToInt32(dataReader["ItemId"]);

            if (dataReader.ContainsKey("Title"))
                model.Title = dataReader["Title"].ToString();

            if (dataReader.ContainsKey("Url"))
                model.Url = dataReader["Url"].ToString();

            if (dataReader.ContainsKey("ImageUrl"))
                model.ImageUrl = dataReader["ImageUrl"].ToString();

            if (dataReader.ContainsKey("DisplayOrder"))
                model.DisplayOrder = Convert.ToInt32(dataReader["DisplayOrder"]);

            if (dataReader.ContainsKey("DateCreated"))
                model.DateCreated = Convert.ToDateTime(dataReader["DateCreated"]);

            if (dataReader.ContainsKey("Description"))
                model.Description = dataReader["Description"].ToString();

            if (dataReader.ContainsKey("Ver"))
                model.Ver = dataReader["Ver"].ToString();

            if (dataReader.ContainsKey("Status"))
                model.Status = (StatusType)Convert.ToInt32(dataReader["Status"]);

            return model;
        }

        #endregion

        #region ILink 成员

        public LinkInfo GetLink(int Id)
        {
            if (Id <= 0)
                return null;

            return linkDAL.GetModel(Id);
        }

        public LinkInfo GetLink(string linkUrl)
        {
            if (string.IsNullOrEmpty(linkUrl))
                return null;


            return linkDAL.GetModel("Link",string.Empty,"Url=@Url","Id desc",DataAccess.CreateParameter("Url",linkUrl));
        }

        public IList<LinkInfo> GetLinks(string searchKey,int itemId)
        {
            string fields = string.Empty;
            StringBuilder where = new StringBuilder();
            IList<IDataParameter> parameters = new List<IDataParameter>();

            if (!string.IsNullOrEmpty(searchKey))
            {
                where.Append(" and (Url like '%' + @SearchKey + '%' or Title like '%' + @SearchKey + '%')");
                parameters.Add(DataAccess.CreateParameter("SearchKey",searchKey));
            }

            if (itemId > 0)
            {
                parameters.Add(DataAccess.CreateParameter("@CategoryId", itemId));
                where.Append(" and exists(select Id from LinkCategories where [Path] + '/' like (select [Path] + '/' from LinkCategories AS LinkCategories_1 where (Id = @CategoryId)) + '%' and (Link.ItemId = Id))");
            }

            if (!string.IsNullOrEmpty(where.ToString()))
                where = where.Remove(0,5);

            IDataParameter[] list = new IDataParameter[parameters.Count];
            parameters.CopyTo(list, 0);

            int total;
            return linkDAL.GetModels(string.Empty,where.ToString() ,"Id",1,999,out total,list);
        }

        public int Insert(LinkInfo info)
        {
           return linkDAL.Insert(
            info,
            "ItemId",
            "Title",
            "Url",
            "ImageUrl",
            "Description",
            "Ver",
            "Status");
        }

        public bool Update(LinkInfo info)
        {
            if (info.Id <= 0)
                return false;

            if (!string.IsNullOrEmpty(info.ImageUrl))
                this.UpdateLoginUrl(info.Id, info.ImageUrl);

            return linkDAL.Update(
                info,
                "Id=@Id",
                "ItemId",
                "Title",
                "Url",
                "Description",
                "Status");
        }

        public bool UpdateDisplayOrder(int Id, int displayOrder)
        {
            return linkDAL.UpdateFields("DisplayOrder", Id, (int)displayOrder);
        }

        public bool Delete(int Id)
        {
            return linkDAL.Delete(Id);
        }

        #endregion

        private bool UpdateLoginUrl(int Id,string ImageUrl)
        {
            return linkDAL.UpdateFields("ImageUrl", Id, ImageUrl);
        }
    }
}
